<template>
  <div class="article-page">
    <!-- 筛选区域 -->
    <el-card>
      <div slot="header">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>发布文章</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表单 -->
      <el-form label-width="80px">
        <el-form-item label="状态：">
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <!--1. :value="resParams.channel_id" 传递频道id给子组件 -->
          <!--2. @input="resParams.channel_id=$event" 监听子组件触发的input事件,$event触发事件的传参 -->
            <!-- <my-channel :value="resParams.channel_id" @input="resParams.channel_id=$event"></my-channel> -->
          <!-- 3. 使用v-model进行双向数据绑定 -->
          <my-channel v-model="resParams.channel_id"></my-channel>
        
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker
            @change="changeDate"
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button @click="filterArticle()" type="primary">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 结果区域 -->
    <el-card style="margin-top: 20px">
      <div slot="header">根据筛选条件共查询到 0 条结果</div>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image
              :src="scope.row.cover.images[0]"
              style="width: 180px; height: 120px"
            >
              <div slot="error">
                <img
                  src="../../assets/error.png"
                  style="width: 180px; height: 120px"
                  alt=""
                />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="标题"
          prop="title"
          width="400px"
        ></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status === 1">待审核</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success"
              >审核通过</el-tag
            >
            <el-tag v-if="scope.row.status === 3" type="warning"
              >审核失败</el-tag
            >
            <el-tag v-if="scope.row.status === 4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate"></el-table-column>
        <el-table-column
          label="阅读数"
          prop="read_count"
          width="120px"
        ></el-table-column>
        <el-table-column
          label="评论数"
          prop="comment_count"
          width="120px"
        ></el-table-column>
        <el-table-column
          label="点赞数"
          prop="like_count"
          width="120px"
        ></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
            ></el-button>
            <el-button
              @click="deleteArticle(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px"
        background
        layout="prev, pager, next"
        @current-change="changePage"
        :current-page="reqParams.page"
        :page-size="reqParams.per_page"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import myChannel from '../../components/my-channel.vue';
export default {
  components: { myChannel },
  name: "ArcticlePage",
  data() {
    return {
      // 请求参数对象
      reqParams: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page: 20,
      },
      // 频道选项
      channelOptions: [],
   //   channelOptions: [],
      // 日期范围 [起始时间,结束时间]
      dateArr: [],
      //文章列表
      articles: [],
      total: 0,
    };
  },
  created() {
  //  this.getChannelOptions();
    this.getArticles();
  },
  methods: {
    //获取频道选项数据
    // async getChannelOptions() {
    //   const res = await this.$http.get("/v1_0/channels");
    //   this.channelOptions = res.data.data.channels;
    // },
    //获取文章列表数据
    async getArticles() {
      //post 传递参数 post(url,参数)
      //get 传递参数 get(url,{params:参数对象})
      const res = await this.$http.get("/v1_0/mp/articles", {
        params: this.reqParams,
      });
      console.log(res);
      this.articles = res.data.data.results; 
      this.total = res.data.data.total_count;
    },
    //切换分页
    changePage(newPage) {
      this.reqParams.page = newPage;
      this.getArticles();
    },
    // 筛选文章
    filterArticle() {
      this.reqParams.page = 1;
      // 在做筛选的时候从第一页开始
      // 获取文章前，收集筛选条件
      // 1. 状态，频道，是自动收集的
      // 2. 日期范围需要手动收集
      this.getArticles();
    },
    // 选择日期
    changeDate(dateArr) {
      // 当你清空的时候，dateArr 是 null , 选择日期后 dateArr = [起始时间，结束时间]
      if (dateArr) {
        this.reqParams.begin_pubdate = dateArr[0];
        this.reqParams.end_pubdate = dateArr[1];
      } else {
        this.reqParams.begin_pubdate = null;
        this.reqParams.end_pubdate = null;
      }
    },
    // 删除
    deleteArticle(id) {
      // 弹出确认框
      this.$confirm("此操作将永久删除该文章, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 点击确认触发then
          // 1. 发起删除请求
          await this.$http.delete(`/v1_0/mp/articles/${id}`);
          // 2. 成功提示
          this.$message.success("删除成功");
          // 3. 更新当前列表
          this.getArticles();
        })
        .catch(() => {
          // 点击确认触发catch
        });
    },
  },
};
</script>
<style scoped lang="less"></style>
